<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>修改药品条目信息</title>
    <script src="../../js/vue.js"></script>
    <script src="../../js/axios.min.js"></script>
    <script src="../../layuiadmin/layui/layui.js"></script>
    <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" media="all">
</head>
<body>
<div id="app">
    <table class="layui-table">
        <colgroup>
            <col width="150">
            <col width="200">
            <col>
        </colgroup>
        <thead>
        <tr>
            <th>药品条目编号</th>
            <th>药品名称</th>
            <th>药品规格</th>
            <th>小计</th>
            <th>当前数量</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            <td>{{mediItemId}}</td>
            <td>{{mediItem.medicine.mediName}}</td>
            <td>{{mediItem.medicine.mediStand}}</td>
            <td>{{mediItem.mediItemSubtotal}}</td>
            <td>{{resetData}}</td>
        </tr>
        </tbody>
    </table>
    <div class="layui-form-item">
        <label class="layui-form-label">选择数量</label>
        <div class="layui-input-inline">
            <input type="text" name="password" required lay-verify="required" v-model:value="mediItem.amount"
                   autocomplete="off"
                   class="layui-input">
        </div>
        <div v-if="isRightNum(mediItem.amount)">
            <div class="layui-form-mid layui-word-aux" style="color:#5FB878!important;">数据正确</div>
        </div>
        <div v-else>
            <div class="layui-form-mid layui-word-aux" style="color:#FF5722!important;">请输入合法数字</div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo" @click="updateMediItem">立即提交</button>
            <button type="reset" @click="resetDataAmount" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</div>
<script>
    var app = new Vue({
        el: "#app",
        data: {
            mediItemId: '',
            mediItem: {},
            amount: 1,
            resetData: 1,
        },
        created() {
            //生命周期函数，一初始化就调用加载数据方法
            this.getData();
            this.amount = this.mediItem.amount;
        },
        methods: {
            isRightNum: function (val) {
                // isNaN()函数 把空串 空格 以及NUll 按照0来处理 所以先去除，
                if (val === "" || val == null) {
                    return false;
                }
                if (!isNaN(val)) {
                    //对于空数组和只有一个数值成员的数组或全是数字组成的字符串，isNaN返回false，例如：'123'、[]、[2]、['123'],isNaN返回false,
                    //所以如果不需要val包含这些特殊情况，则这个判断改写为if(!isNaN(val) && typeof val === 'number' )
                    return true;
                } else {
                    return false;
                }
            },
            //更新条目和处方信息
            updateMediItem: function () {
                let params = "?mediItemId=" + this.mediItemId + "&";
                params += "amount=" + this.mediItem.amount;
                if (this.isRightNum(this.mediItem.amount)) {
                    var that=this;
                    //数据正确返回后台更新数据
                    axios.get("http://localhost:8080/mediItem/updateMediItemById" + params).then(function (resp) {
                        if (resp.data.data === "success") {
                            alert("更新成功");
                            window.location.href="recipe.html?recipeMediId="+that.mediItem.recipeMedi.recipeMediId;
                        } else {
                            alert("更新失败，系统服务维护中")
                        }
                    });
                }
            }
            ,
            //重置数据
            resetDataAmount: function () {
                this.mediItem.amount = this.resetData;
            },
            getData: function () {
                let id = this.getUrlKey("mediItemId");
                this.mediItemId = id;
                var that = this;
                axios.get("http://localhost:8080/mediItem/findMediItemById?mediItemId=" + id).then(function (resp) {
                    that.mediItem = resp.data.data;
                    that.resetData = resp.data.data.amount;
                });
            }
            ,
            dateFormat: function (value, fmt) {
                let getDate = new Date(value);
                let o = {
                    'M+': getDate.getMonth() + 1,
                    'd+': getDate.getDate(),
                    'h+': getDate.getHours(),
                    'm+': getDate.getMinutes(),
                    's+': getDate.getSeconds(),
                    'q+': Math.floor((getDate.getMonth() + 3) / 3),
                    'S': getDate.getMilliseconds()
                };
                if (/(y+)/.test(fmt)) {
                    fmt = fmt.replace(RegExp.$1, (getDate.getFullYear() + '').substr(4 - RegExp.$1.length))
                }
                for (let k in o) {
                    if (new RegExp('(' + k + ')').test(fmt)) {
                        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
                    }
                }
                return fmt;
            }
            ,
            getUrlKey: function (name) {
                //构造一个含有目标参数的正则表达式对象
                var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
                //匹配目标参数
                var r = window.location.search.substr(1).match(reg);
                //返回参数
                if (r != null) {
                    return unescape(r[2]);
                } else {
                    return null;
                }
            }
        }
    })
</script>
</body>
</html>